package com.qfedu.video.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

// ctrl + o
public class LoginInterceptor implements HandlerInterceptor {
    /*
        handle 意思是controller中的方法
        handle方法调用之前执行，一般用于登录校验
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();
        Object userName = session.getAttribute("userName");
        if(userName!=null && !userName.equals("")){
            return true;// 放行
        }
        //在此处跳转页面还是在其他地方
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/admin/showLogin");


        return false;//被拦截，资源访问不到
    }

    /**
     * 方法调用之后，视图返回之前
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 方法执行完毕，页面也展示出来了，后面执行该方法
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
